package com.example.service.impl;

import com.example.mapper.EmpExprMapper;
import com.example.mapper.EmpMapper;
import com.example.params.EmpQueryParam;
import com.example.pojo.Emp;
import com.example.pojo.EmpExpr;
import com.example.pojo.LoginInfo;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import com.example.vo.empvo.EmpListVo;
import com.example.vo.empvo.GetAllEmpInfoVo;
import com.example.vo.report.JobOption;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    public GetAllEmpInfoVo getAllEmpInfo(EmpQueryParam queryParam) {
        PageHelper.startPage(queryParam.getPage(),queryParam.getPageSize());
        List<Emp> row = empMapper.getAllEmps(queryParam.getName(), queryParam.getGender(), queryParam.getBegin(), queryParam.getEnd());
        Page<Emp> p = (Page<Emp>) row;

        //1. 设置分页参数
        //3. 获取分页结果
        //PageInfo<Emp> pageInfo = new PageInfo<>(row);

        return new GetAllEmpInfoVo(p.getTotal(),p.getResult());
    }

    public List<EmpListVo> getAllEmpList() {
        List<Emp> empList = empMapper.getAllEmpList();
        List<EmpListVo> empListVos = new ArrayList<>();
        for (Emp emp : empList) {
            empListVos.add(new EmpListVo(emp.getId(), emp.getUsername(), emp.getPassword(), emp.getName(), emp.getGender(), emp.getJob(), emp.getSalary(), emp.getImage(), emp.getEntryDate(), emp.getDeptId(), emp.getCreateTime(), emp.getUpdateTime()));
        }

        return empListVos;
    }
    @Transactional
    public void deleteEmpByIds(List<Integer> ids) {
        empMapper.deleteEmpById(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    public Emp getEmpInfoById(Integer id) {
        Emp emp = empMapper.getEmpById(id);
        return emp;
    }
    @Transactional
    public void updateById(Emp emp) {
        //1.更新员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2.删除旧的工作经历
        List<Integer> empIds = new ArrayList<>();
        empIds.add(emp.getId());
        empExprMapper.deleteByEmpIds(empIds);
        //3.新增新的工作经历
        Integer empId = emp.getId();
        for (EmpExpr empExpr : emp.getExprList()) {
            empExpr.setEmpId(empId);
            empExprMapper.insert(empExpr);
        }
    }

    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);

        if(empLogin != null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",empLogin.getId());
            claims.put("username",empLogin.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
    @Transactional
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        for (EmpExpr empExpr : exprList) {
            empExpr.setEmpId(empId);
        }
        empExprMapper.insertBatch(exprList);
    }

}
